  <template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card" style="margin-top: 20px">
      <el-button @click="tianjia" type="primary" style="margin-bottom: 20px"
        >添加角色</el-button
      >
      <el-table :data="jueselist" style="width: 100%" border>
        <el-table-column type="expand">
          <template slot-scope="{ row }">
            <el-row :key="item.id" v-for="item in row.children">
              <el-col :span="5">
                <el-tag
                  @close="close(row.children, item.id, row.id)"
                  closable
                  type
                  >{{ item.authName }}</el-tag
                >
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <el-row :key="item1.id" v-for="item1 in item.children">
                  <el-col :span="6">
                    <el-tag
                      @close="close(item.children, item1.id, item.id)"
                      closable
                      type="success"
                      >{{ item1.authName }}</el-tag
                    >
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      :key="item2.id"
                      @close="close(item1.children, item2.id, item1.id)"
                      closable
                      type="warning"
                      v-for="item2 in item1.children"
                      >{{ row.roleName }}</el-tag
                    >
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column label="序号" type="index" width="50">
        </el-table-column>
        <el-table-column label="角色名称" prop="roleName"> </el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"> </el-table-column>
        <el-table-column label="操作" prop="desc">
          <template slot-scope="{ row }">
            <el-button type="primary">
              <i class="el-icon-edit">编辑</i>
            </el-button>
            <el-button type="danger">
              <i @click="del(row)" class="el-icon-delete">删除</i>
            </el-button>
            <el-button type="warning">
              <i @click="open(row)" class="el-icon-setting">分配权限</i>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分配对话框 -->
      <el-dialog :visible.sync="dialogVisible" title="分配权限" width="30%">
        <el-tree
          :data="data"
          :props="defaultProps"
          default-expand-all
          highlight-current
          node-key="id"
          ref="tree"
          show-checkbox
        ></el-tree>
        <span class="dialog-footer" slot="footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button @click="quanxianfenpei" type="primary">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 添加角色 -->
      <el-dialog
        :visible.sync="juesedialogVisible"
        title="添加角色"
        width="30%"
      >
        <!-- <span>这是一段信息</span> -->
        <el-form :model="jueseform" label-width="100px">
          <el-form-item label="角色名称">
            <el-input v-model="jueseform.roleName"></el-input>
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="jueseform.roleDesc"></el-input>
          </el-form-item>
        </el-form>
        <span class="dialog-footer" slot="footer">
          <el-button @click="juesedialogVisible = false">取 消</el-button>
          <el-button @click="postjuse" type="primary">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>
  <script>
import { getRolesAPI, getquanxianApi, delquanxianApi, postquanxianApi, postjuseApi } from '@/api/roles'
export default {
  name: '',
  data () {
    return {
      jueselist: [],
      quanxianlist: [],
      data: [],
      dialogVisible: false,
      juesedialogVisible: false,
      id: 0,
      defaultProps: {
        children: 'children',
        label: 'authName'
      },
      jueseform: {
        roleName: '',
        roleDesc: ''
      }
    }
  },
  created () {
    this.getRoles()
    this.getqianxian()
  },
  computed: {
  },
  methods: {
    async getRoles () {
      const res = await getRolesAPI()
      console.log(res)
      this.jueselist = res.data
    },
    async getqianxian () {
      const res = await getquanxianApi()
      console.log(res, 56565)
      this.data = res.data
    },
    async postjuse () {
      const res = await postjuseApi(this.jueseform)
      console.log(res, 97979)
      if (res.meta.status !== 201) {
        return this.$message.error('添加失败')
      }
      this.$message.success('添加成功')
      this.juesedialogVisible = false
      this.getRoles()
    },
    tianjia () {
      this.juesedialogVisible = true
    },
    close (arr, id1, id2) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log(id1, id2)
        arr.splice(arr.findIndex(ele => ele.id1 === id1), 1)
        delquanxianApi(id2, id2)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    open (row) {
      this.dialogVisible = true
      this.id = row.id
      this.$nextTick(() => {
        this.$refs.tree.setCheckedNodes(row.children)
      })
      console.log(this.id, 19191)
    },
    async quanxianfenpei () {
      const checkedNodes = this.$refs.tree.getCheckedNodes(false, true)
      const checkedIds = checkedNodes.map(node => node.id)

      const res = await postquanxianApi(this.id, { rids: checkedIds.join() })
      console.log(res, 8787)
      this.dialogVisible = false
      this.getRoles()
      if (res.meta.status === 200) {
        return this.$message.success('分配成功')
      }
    },
    async del (row) {
      console.log(row, 8989)
      const res = await delquanxianApi(row.id)
      console.log(res, 7878)
      if (res.meta.status !== 200) {
        return this.$message.error('删除失败')
      }
      this.$message.success('删除成功')
      this.getRoles()
    }
  }
}
  </script>
  <style lang='less'  scoped>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: '';
}
.clearfix:after {
  clear: both;
}

.box-card {
  margin-top: 20px;
  width: 100%;
}
</style>
